<?php

namespace App\Services\Api;

use App\Models\UserIntegralUsable;
use Illuminate\Support\Facades\DB;
use App\Models\UserIntegralChangeRecord;
use App\Models\IntegralPlan;
use App\Models\UserIntegral;
use App\Services\Admin\UserIntegral as ServicesUserIntegral;
use App\Models\User;

class UserIntegralServices
{

    public function available_integral($user_id)
    {
        $member_id = $user_id;
        $time = date("Y-m-d H:i:s");
        /*  $integral = UserIntegralUsable::where('user_id',$member_id)->where('expire_time','>=',$time)->sum('integral');*/
        $integral = User::where('id', $member_id)->where('expire_time', '>=', $time)->select(['expire_time', 'total_integral'])
            ->first();

        return $integral->total_integral ?? 0;
    }

    public function use_integral($user_id, $use_integral, $msg = '')
    {
        //dd($user_id,$use_integral);

        DB::beginTransaction();
        try {

            $user_integral = $this->available_integral($user_id);
            if ($use_integral > $user_integral) {
                return false;
            }

            $integral = $user_integral - $use_integral;
            //dd($integral,$user_integral,$use_integral);
            if ($integral < 0) {
                $integral = 0;
            }
            $res = User::where('id', $user_id)->update(['total_integral' => $integral]);
            $record['user_id'] = $user_id;
            $record['integral'] = $integral;
            $record['use_integral'] = $use_integral;
            $record['chage_type'] = 2;
            //$record['original_id'] ='';
            //$record['expire_time'] = $vv['expire_time'];
            //$record['expire_time'] = $vv['expire_time'];
            $record['reason'] = $msg;
            $record['created_at'] = date("Y-m-d H:i:s");
            //扣减记录
            UserIntegralChangeRecord::insert($record);

            DB::commit();
            return true;
        } catch (\Exception $e) {

            DB::rollBack();
            return false;
        }

    }

    public function add_integral($user_integral_id, $param = [])
    {

        DB::beginTransaction();
        try {

            $userIntegra = ServicesUserIntegral::info($user_integral_id);

            if ($userIntegra->status != 1) throw new \Exception();

            UserIntegral::where('id', $user_integral_id)->update(['status' => $param['status']]);

            $userIntegralObj = UserIntegral::where('id', $user_integral_id)->first();

            $UserObj = User::where('id', $userIntegralObj->user_id)->first();

            //dd($UserObj->expire_time);

            if (strtotime($UserObj->expire_time) > 0 && !empty($UserObj->expire_time)) {
                $expire_time = date("Y-m-d H:i:s", strtotime($UserObj->expire_time) + $userIntegralObj->valid_total_time);
            } else {
                $expire_time = date("Y-m-d H:i:s", time() + $userIntegralObj->valid_total_time);
            }

            if ($userIntegralObj->status == 2) {
                $data = [
                    'user_id' => $userIntegralObj->user_id,
                    'integral' => $userIntegralObj->integral + $UserObj->getOriginal('total_integral'),
                    'use_integral' => $userIntegralObj->integral,
                    'chage_type' => 1,
                    'expire_time' => $expire_time,
                    'reason' => '購買獲得积分',
                    'created_at' => date("Y-m-d H:i:s"),
                ];
                UserIntegralChangeRecord::insert($data);

                $new_data = [
                    'user_id' => $userIntegralObj->user_id,
                    'user_integral_apply_id' => $userIntegralObj->id,
                    'user_integral__change_recode_id' => 0,
                    'integral' => $userIntegralObj->integral,
                    'expire_time' => $expire_time,
                    'remarks' => '審核添加',
                    'state' => 1,
                    'created_at' => date("Y-m-d H:i:s"),
                ];
                UserIntegralUsable::insert($new_data);

                $UserObj->total_integral = $userIntegralObj->integral + $UserObj->getOriginal('total_integral');
                $UserObj->expire_time = $expire_time;
                $UserObj->save();

            }
            DB::commit();
            return true;
        } catch (\Exception $e) {
            DB::rollBack();
            dd($e->getMessage());
            return false;
        }

    }

}
